package com.situ.mall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.mall.entity.PageView;
import com.situ.mall.entity.Product;
import com.situ.mall.entity.Result;
import com.situ.mall.entity.Shipping;
import com.situ.mall.entity.Suggest;
import com.situ.mall.entity.User;
import com.situ.mall.mapper.ShippingMapper;
import com.situ.mall.mapper.UserMapper;
import com.situ.mall.service.IUserService;

@Service
public class UserServiceImpl implements IUserService{
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private ShippingMapper shippingMapper;

	@Override
	public User login(String username, String password) {
		return userMapper.login(username, password);
	}

	@Override
	public User selectByUserId(Integer id) {
		User user = userMapper.selectByUserId(id);
		return user;
	}

	@Override
	public boolean updateUserInfo(User user) {
		int count = userMapper.updateByPrimaryKey(user);
		return count == 1 ? true : false;
	}
	
	@Override
	public boolean updateUser(User user) {
		int count = userMapper.updateByPrimaryKey(user);
		return count == 1 ? true : false;
	}

	@Override
	public Result selectByPage(Integer page, Integer limit,User user) {
		System.out.println("UserServiceImpl.selectByPage()");
		// 查询一种有多少条数据
		int count = userMapper.getTotalCount();
		// 当前页数据
		int offset = (page -1 ) * limit;
		List<User> list = userMapper.selectByPage(offset, limit,user);
		for (User user1 : list) {
			System.out.println(user);
		}
		return Result.success("", list, count);
	}

	//前台注册用户，只能添加
	@Override
	public boolean register(User user) {
		int count = userMapper.insert(user);
		return count == 1 ? true : false;
	}

	//后台添加用户
	@Override
	public boolean insertUser(User user) {
		int count = userMapper.insert(user);
		return count == 1 ? true : false;
	}

	@Override
	public boolean deleteById(Integer id) {
		int count = userMapper.deleteByPrimaryKey(id);
		return count == 1 ? true : false;
	}

	@Override
	public User getUpdateUserInfo(Integer userId) {
		User user = userMapper.selectByUserId(userId);
		return user;
	}

	@Override
	public User selectByUserName(String username,Integer userId) {
		User user = userMapper.selectByUserName(username,userId);
		return user;
	}

	@Override
	public boolean suggest(Suggest suggest) {
		int count = userMapper.insertSuggest(suggest);
		return count == 1 ? true : false;
	}

	@Override
	public User registerByUserName(String username) {
		User user = userMapper.registerByUserName(username);
		return user;
	}

	@Override
	public boolean deleteAll(Integer[] ids) {
		int count = 0;
		for (Integer id : ids) {
			// 每删除一个影响一行c返回就是1
			int c = userMapper.deleteByPrimaryKey(id);
			count += c;
		}
		
		//int count = productMapper.deleteAll(ids);
		return count == ids.length ? true : false;
	}

	@Override
	public PageView getPageViewById(Integer id) {
		// TODO Auto-generated method stub
		return userMapper.getPageViewById(id);
	}
 
 
	@Override
	public int updatePageView(Integer id, PageView pageView) {
		// TODO Auto-generated method stub
		return userMapper.updatePageView(id, pageView);
	}
	

	

}
